package com.dswz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dswz.dao.UserCartDao;
import com.dswz.po.UserCart;
import com.dswz.po.UserCartQuery;
import com.dswz.service.UserCartService;
import com.dswz.utils.PageBean;

@Service("userCartService")
@Transactional
public class UserCartServiceImpl implements UserCartService {
	private Logger logger = LogManager.getLogger(UserCartServiceImpl.class);

	@Autowired
	private UserCartDao userCartDao;

	@Transactional(readOnly = true)
	public PageBean getUserCartList(String username, String userId, Byte isDel, Integer page, Integer size)
			throws Exception {
		int count = 0;
		List<UserCart> userCartList = new ArrayList();
		UserCartQuery userCartQuery = new UserCartQuery();

		userCartQuery.setPageNo(page);

		userCartQuery.setPageSize(size);

		userCartQuery.setOrderByClause("user_cart_create_time desc");
		if ("admin".equals(username)) {
			UserCartQuery.Criteria createCriteria = userCartQuery.createCriteria();

			if (StringUtils.isNotEmpty(userId)) {
				createCriteria.andUserIdEqualTo(userId);
			}

			if (isDel != null) {
				createCriteria.andUserCartIsDelEqualTo(isDel);
			}
			count = this.userCartDao.countByExample(userCartQuery);
			userCartList = this.userCartDao.selectByExample(userCartQuery);
		} else {
			UserCartQuery.Criteria createCriteria = userCartQuery.createCriteria();

			if (StringUtils.isNotEmpty(userId)) {
				createCriteria.andUserIdEqualTo(userId);
			}

			if (isDel != null) {
				createCriteria.andUserCartIsDelEqualTo(isDel);
			}
			count = this.userCartDao.countByExample(userCartQuery);
			userCartList = this.userCartDao.selectByExample(userCartQuery);
		}
		PageBean pb = new PageBean(size.intValue(), count, page.intValue() + 1);

		pb.setContentList(userCartList);
		return pb;
	}

	public void insertUserCart(UserCart userCart) throws Exception {
		if (userCart.getUserCartOrderNo() == null) {
			userCart.setUserCartOrderNo(Integer.valueOf(0));
		}

		userCart.setUserCartIsDel(Byte.valueOf((byte) 0));

		userCart.setUserCartCreateTime(new Date());
		this.userCartDao.insertSelective(userCart);
	}

	public void deleteUserCarts(List<String> userCartIds) throws Exception {
		UserCart userCart = new UserCart();
		userCart.setUserCartIsDel(Byte.valueOf((byte) 1));
		if ((userCartIds != null) && (userCartIds.size() > 0)) {
			for (String userCartId : userCartIds) {
				userCart.setUserCartId(userCartId);
				this.userCartDao.updateByPrimaryKeySelective(userCart);
			}
		}
	}

	public void deleteUserCart(String userCartId) throws Exception {
		UserCart userCart = new UserCart();
		userCart.setUserCartIsDel(Byte.valueOf((byte) 1));
		userCart.setUserCartId(userCartId);
		this.userCartDao.updateByPrimaryKeySelective(userCart);
	}

	public void updateUserCart(UserCart userCart) throws Exception {
		this.userCartDao.updateByPrimaryKeySelective(userCart);
	}

	@Transactional(readOnly = true)
	public UserCart getUserCartById(String id) throws Exception {
		return this.userCartDao.selectByPrimaryKey(id);
	}

	@Transactional(readOnly = true)
	public List<UserCart> getUserCartList(String goodsId, Byte isDel) throws Exception {
		UserCartQuery userCartQuery = new UserCartQuery();

		UserCartQuery.Criteria createCriteria = userCartQuery.createCriteria();
		createCriteria.andGoodsIdEqualTo(goodsId);

		if (isDel != null) {
			createCriteria.andUserCartIsDelEqualTo(isDel);
		}
		List<UserCart> userCartList = this.userCartDao.selectByExample(userCartQuery);
		return userCartList;
	}
}